﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;

namespace BridgeUI.Editors
{
    public class Menus
    {
        private const string menu01 = "CONTEXT/Transform/添加引用脚本（主文件）";
        private const string menu02 = "CONTEXT/Transform/添加引用脚本（子文件）";
        private const string menu03 = "CONTEXT/BindingReference/编辑绑定";

        [MenuItem(menu01)]
        private static void QuickCreateBindingReferenceScript(MenuCommand command)
        {
            var transform = command.context as Transform;
            if (transform != null)
            {
                var component = transform.GetComponent<BridgeUI.Binding.BindingReference>();

                if (component == null)
                {
                    var nameSpace = BridgeUI.UISetting.defultNameSpace;
                    var scriptName = transform.name + "Reference";
                    var fullName = string.IsNullOrEmpty(nameSpace) ? scriptName : nameSpace + "." + scriptName;
                    var oldScript = BridgeUI.Utility.GameAssembly.GetType(fullName);
                    if (oldScript != null)
                    {
                        if (typeof(Component).IsAssignableFrom(oldScript))
                        {
                            transform.gameObject.AddComponent(oldScript);
                        }
                        else
                        {
                            Debug.LogError("已经存在名为" + scriptName + "的非component脚本！！！");
                        }
                    }
                    else
                    {
                        var scriptPath = string.Format("{0}/{1}/{2}.cs", BridgeUI.UISetting.script_path, transform.name, scriptName);
                        CreateBindingReferenceScriptToPath(nameSpace, scriptName, scriptPath);
                        AssetDatabase.Refresh();
                    }
                }
            }
            else
            {
                EditorUtility.DisplayDialog("提示", "按GameObject的名字创建引用代码", "OK");
            }
        }

        [MenuItem(menu02)]
        private static void QuickCreateBindingReferenceScript_Sub(MenuCommand command)
        {
            var transform = command.context as Transform;
            if (transform != null)
            {
                var component = transform.GetComponent<BridgeUI.Binding.BindingReference>();

                if (component == null)
                {
                    var nameSpace = BridgeUI.UISetting.defultNameSpace;
                    var scriptName = transform.name + "Reference";

                    var fullName = string.IsNullOrEmpty(nameSpace) ? scriptName : nameSpace + "." + scriptName;
                    var oldScript = BridgeUI.Utility.GameAssembly.GetType(fullName);
                    if (oldScript != null)
                    {
                        if (typeof(Component).IsAssignableFrom(oldScript))
                        {
                            transform.gameObject.AddComponent(oldScript);
                        }
                        else
                        {
                            Debug.LogError("已经存在名为" + scriptName + "的非component脚本！！！");
                        }
                    }
                    else
                    {
                        var scriptPath = string.Format("{0}/{1}/{2}.cs", BridgeUI.UISetting.script_path, transform.name, scriptName);

                        var parentReference = transform.gameObject.GetComponentInParent<BridgeUI.Binding.BindingReference>();
                        if (parentReference != null)
                        {
                            var folder = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(MonoScript.FromMonoBehaviour(parentReference)));
                            scriptPath = string.Format("{0}/{1}.cs", folder, scriptName);
                        }

                        CreateBindingReferenceScriptToPath(nameSpace, scriptName, scriptPath);
                        AssetDatabase.Refresh();
                    }
                }
            }
            else
            {
                EditorUtility.DisplayDialog("提示", "按GameObject的名字创建引用代码", "OK");
            }
        }

        [MenuItem(menu03, false, 1)]
        public static void CONTEXT_PanelBase_LoadDefultViewModel(MenuCommand command)
        {
            var component = command.context as Binding.BindingReference;
            if (component != null)
            {
                EditorWindow.GetWindow<BindingWindow>().OpenWith(component);
            }
        }

        private static void CreateBindingReferenceScriptToPath(string nameSpace, string scriptName, string scriptPath)
        {
            var folder = System.IO.Path.GetDirectoryName(scriptPath);

            if (!System.IO.Directory.Exists(folder))
            {
                System.IO.Directory.CreateDirectory(folder);
            }

            var scriptStr = Binding.Editors.BindingReferenceEditor.CreateScript(nameSpace, scriptName, new List<BridgeUI.Binding.ReferenceItem>());

            System.IO.File.WriteAllText(scriptPath, scriptStr);
        }
    }
}